﻿using System;
using System.Web;
using System.Web.UI;
using System.Linq;
using Models;
using System.Web.UI.WebControls;

public partial class Add : AdminBase
{
    public int _Id = 0;

    protected void Page_Load(object sender, EventArgs e)
    {

        Response.CacheControl = "no-cache";

        if (!string.IsNullOrEmpty(Request.Params["ID"]))
        {
            _Id = int.Parse(HttpContext.Current.Request.QueryString["ID"]);

        }


        if (!Page.IsPostBack)
        {
           SCClassTreeBind("一级分类...", this.aClassddl);

            if (_Id != 0)
            {
                ShowInfo(_Id);
            }

        }


    }

    //保存
    protected void btnSave_Click(object sender, EventArgs e)
    {
        Entities mydb = new Entities();
        {
            S_Classs lx = new S_Classs();
            if (_Id != 0)
                lx = mydb.S_Classs.First(p => p.SCID == _Id);


            lx.SCNmae = txtName.Text.Trim();


            lx.SCSIMG = xsimg.Value.Trim();//图片

            lx.SCpareID = short.Parse(aClassddl.SelectedValue);
            lx.SCType = 1;

            if (_Id == 0)
            {
                mydb.S_Classs.Add(lx);
                mydb.SaveChanges();
                Response.Write("<script> var success=1;</script>");

            }
            else
            {
                mydb.SaveChanges();
                Response.Write("<script> var success=1;</script>");
            }
        }


        // bll.Add(model);
        //保存日志
        //SaveLogs("[资讯模块]添加文章：" + model.Title);
        //JscriptPrint("文章发布成功啦！", "Add.aspx?classId=" + ddlClassId.SelectedValue, "Success");
    }

    private void ShowInfo(int _id)
    {

        Entities mydb = new Entities();
        {

            S_Classs _lx = mydb.S_Classs.First(p => p.SCID == _id);
            txtName.Text = _lx.SCNmae;
            imgArr = getImg(_lx.SCSIMG);
            aClassddl.SelectedValue = _lx.SCpareID.ToString();

        }
    }


    /// <summary>
    /// 绑定类别DropDownList控制
    /// </summary>
    /// /// <param name="firstItemTxt">第一项显示的文字</param>
    /// <param name="ddl">要绑定的DropDownList控件</param>

    public void SCClassTreeBind(string firstItemTxt, DropDownList dp)
    {
        string tepmValue = "0";
        SCClassTreeBindFV(tepmValue, firstItemTxt, dp);

    }
    public void SCClassTreeBindFV(string firstItemValue, string firstItemTxt, DropDownList dp)
    {
        dp.Items.Clear();
        ListItem list = new ListItem(firstItemTxt, firstItemValue);
        dp.Items.Add(list);

        using (Entities _class = new Entities())
        {
            int a;
            int.TryParse(list.Value, out a);

            var m = from n in _class.S_Classs where n.SCpareID == a select n;
            foreach (var n in m)
            {


                dp.Items.Add(new ListItem(n.SCNmae.ToString(), n.SCID.ToString()));
                int leng = 1;

                dpbinder(dp, int.Parse(n.SCID.ToString()), leng);

            }
        }
    }

}

